package com.godlei.oauth2.controller;

import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @ClassName OAuth2Controller
 * @Description OAuth2集成三方认证Controller
 * @Author 高达磊
 * @Date 2021/12/22-17:38
 * @Version 1.0
 */
@Controller
public class OAuth2Controller {

    @GetMapping("/")
    public String toIndex(Model model,
                          @RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient oAuth2AuthorizedClient,
                          @AuthenticationPrincipal OAuth2User oAuth2User) {
        model.addAttribute("username", oAuth2User.getName());
        model.addAttribute("clientName", oAuth2AuthorizedClient.getClientRegistration().getClientName());
        model.addAttribute("userAttributes", oAuth2User.getAttributes());
        return "index";
    }
}
